<%
// Creation History
// CRLOG# 1296
// --------------------------------------------------------------
//  Company:	HTHK
//	NAME   :	usimheader.jsp
//	TITLE  :	A new header.jsp for USIM
//	FUNCTION:	Check session value and Working Company
//	BY     :	Winson Jian
//	DATE   :	14/10/2004
//--------------------------------------------------------------
// Modification History
//--------------------------------------------------------------
//  
//--------------------------------------------------------------
//CR log no 1054: By Oscar Bai  Date:2003/02 (for previous file header.jsp)
//CR log no 1296: By Winson Jian  Date:2004/09
//******************************************************************/
%>

<%@ page import="com.ffpc.struts.StrutsMessage" %>
<%@ page import="java.sql.*" %>
<%@ page import="java.util.*" %>
<%@ page import="java.text.*" %>
<%@ page import="java.net.URLEncoder" %>
<%@ page import="java.net.URLDecoder" %>
<%@ page import="admin.*" %>
<%@ page import="com.ffpc.sql.*"%>
<%@ page import="usim.admin.datamap.*" %>
<%
  if ( session.getAttribute("user") == null ) {
    System.out.println("usimheader check user is null ");
%>
    <jsp:forward page='/servlet/admin.LogoutServlet'/>
<%
  }

  // 30/09/2002 end
    ConnectionFactory connectionFactory = new ConnectionFactory();
    Connection con = (Connection)connectionFactory.getConnection();
    StrutsMessage message = (StrutsMessage)request.getAttribute("message");
    String UsimWorkingCompany = "";
    if (message==null)
       message = new StrutsMessage();
    String contextPath = request.getContextPath();
    
    String formAction = request.getParameter("formAction");
    
    String readOnly = new String("");
    if (formAction!=null && formAction.trim().equalsIgnoreCase("Update"))
       readOnly = new String(" readonly");
       
    User user = (User)session.getAttribute("user");
    String sid = connectionFactory.getSID();
    if (sid=="csms") {sid="prod";}
    String costCenter="";
    try {
     	DataMapBean[] dataMaps;
     	DataMapBean dataMap;
     	costCenter = user.getCost_center_id();      	
    	
      UsimWorkingCompany = (String) session.getAttribute("UsimWorkingCompany");
      if( UsimWorkingCompany == null ){
      	
      	dataMap=DataMapProcess.createBean();
      	dataMap.setCost_center_id(costCenter);
      	dataMaps = DataMapProcess.find(con,dataMap);
      	if( dataMaps == null ){
      		throw new Exception();
      	}
      	else if(dataMaps.length == 1){
      		session.setAttribute("UsimWorkingCompany",dataMaps[0].getBusiness_type());
      		UsimWorkingCompany = (String) session.getAttribute("UsimWorkingCompany");
      	}
      	else{
      		//request.setAttribute("dataMaps",dataMaps)
%>
                <jsp:forward page='/usim/code/ChangeWorkingCompany.jsp'/>
<%
      	}
      }
    }
    catch (Exception e) {
      System.out.println("USIM Qheader error = " + e.toString() );
    }
    finally {

    }
%>

<LINK rel='stylesheet' type='text/css' href='<%=contextPath%>/style.css'>
<head>
   <title>Hutchison - USIM  Application</title>
   <META HTTP-EQUIV="expires" CONTENT="0">
   <META HTTP-EQUIV="Pragma" CONTENT="no-cache">
   <META HTTP-EQUIV="Cache-Control" CONTENT="no-store">
</head>
<table width=100% cellpadding=0 cellspacing=0>
<tr>
<td align=left style="color:white;background-color:#00AADD;" > * - Mandatory Field</td>
<td align=right style="color:white;background-color:#00AADD;" >Welcome &nbsp; &nbsp;<b><i><%= UsimWorkingCompany %> - <%=user.getUser_name()%> - <%=user.getCost_center_id()%></i></b></td>
</tr>
</table>
<% if (message.getMessage()!=null && message.getMessage().trim().length()>3 )
   { %>
  <script lang=javascript >
          alert('<%= message.getMessage() %>');
  </script>
<%   
  //CR log no 1054 begin of: Don't show message dialog when the page back;

  message.setMessage("");
  
  //CR log no 1054 end of;
  }
%>

<script language=javascript>

 function Tokenizer(string, separator, pos)
 {
    var oText = string;
    var nText = null;
    for (var i=0; i < pos; i++)
    {
       if (oText.indexOf(separator)==-1)
       {
          nText = oText
       }
       else
       {
          nText = oText.substring(0, oText.indexOf(separator));
          oText = oText.substring(oText.indexOf(separator)+1);
       }
    }
    return nText;
 }

 function isNull(object,desc) {
          var obj=object;
          if (obj.value==null||obj.value.length==0) {
             alert(desc + " cannot be null! ");
             return true;
          }
          return false;
 }

 function isInt(object) {
    var obj=object;
    if (parseInt(obj.value)==parseFloat(obj.value)) {
      return true;
    }
      return false;
 }

 function isNotNumeric(object,desc) {
          var obj=object;

          if (obj.value!=null && isNaN(obj.value)) {
             alert(desc + "  must be numeric!");
             return true;
          }
          return false;
 }

// CR1054 This is a more comprehensive function for date validation
// and can be used to replace isDate() below

 function validationDate(dateStr) {

    var datePat = /^(\d{1,2})(\/)(\d{1,2})(\/)(\d{4})$/;
    var matchArray = dateStr.match(datePat);

    if (matchArray == null) {
        return false;
    }

    day = matchArray[1];
    month = matchArray[3];
    year = matchArray[5];

    if (day < 1 || day > 31) {
        return false;
    }

    if (month < 1 || month > 12) {
        return false;
    }

    if ((month==4 || month==6 || month==9 || month==11) && day==31) {
        return false;
    }

    if (month == 2) {
        var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
        if (day > 29 || (day==29 && !isleap)) {
            return false;
        }
    }
    return true;
}

 function isDate(object,desc) {
          var obj=object;
          var d = obj.value;
          if (chLen(obj," date field (dd/MM/yyyy) ",10)==false)
             return false;
          if (d.substring(2,3)!="/" || d.substring(5,6)!="/") {
             alert(" Invalid Date Format.");
             return false;
          }
          var day = d.substring(0,2);
          var month = d.substring(3,5);
          var year = d.substring(6,10);
          if (day==null || isNaN(day)) {
             alert(" Invalid Date Format.");
             return false;
          }
          if (month==null || isNaN(month)) {
             alert(" Invalid Date Format.");
             return false;
          }
          if (day==null || isNaN(day)) {
             alert(" Invalid Date Format.");
             return false;
          }
          if (parseInt(day)>31) {
             alert(" Invalid Date Format.");
             return false;
          }
          if (parseInt(month)>12) {
             alert(" Invalid Date Format.");
             return false;
          }
          return true;
 }


 function chLen(object,desc,len) {
          var obj=object;

          if (obj.value!=null && obj.value.length!=len) {
             alert("The field length of " + desc + "  must be " + len + "! ");
             return false;
          }
          return true;
 }

 function deleteRecord() {
          if (fm.formAction.value!="Update")
          {
                alert('This is a new record.');
                return;
          }
          if (confirm("Are you sure to delete this record?"))
          {

             fm.formAction.value="Delete";
             fm.submit();
          }
 }

 function save() {
          if (confirm("Are you sure to save this record ?"))
          {
             if (!formValidation())
                return false;
             fm.submit();
          }
 }

 function search() {
             if (!formValidation())
                return;
             fm.submit();
 }

 function spsubmit() {
          if (confirm("Are you sure to submit this request ?"))
          {
             if (!formValidation())
                return;
             fm.submit();
          }
 }

 function addNewRow(targetTable,newRowTemplate){
    var newRow = targetTable.insertRow();
    for(var i=0; i<newRowTemplate.rows(0).cells.length; i++){
      newRow.insertCell().innerHTML = newRowTemplate.rows(0).cells(i).innerHTML;
    }
    return newRow;
 }

 function delRows(targetTable,startRow,checkCellIndex,checkChildIndex,statusCellIndex,statusChildIndex,statusForDelete,deleteStatus) {
  if (confirm("Are you to delete this record ?")) {
    var checkCell   = checkCellIndex;
    var checkChild  = checkChildIndex;
    var statusCell  = statusCellIndex;
    var statusChild = statusChildIndex;
    for (var i=startRow; i<targetTable.rows.length; i++ ) {
      if (targetTable.rows(i).cells(checkCell).children(checkChild).checked) {
         if (targetTable.rows(i).cells(statusCell).children(statusChild).value!=statusForDelete) {
            targetTable.deleteRow(i);
            i = i - 1;
         } else {
           targetTable.rows(i).style.display = 'none';
           targetTable.rows(i).cells(statusCell).children(statusChild).value = deleteStatus;
         }
      }
    }
  }
 }

 function delAllRows(targetTable) {
    for (var i=0; i<targetTable.rows.length; i++ ) {
        targetTable.deleteRow(i);
        i = i - 1;
    }
 }

 function delRow(targetTable,rownum) {
    targetTable.deleteRow(rownum);
 }

 function encode(s) {
    t="";
    for (var i=0;i<s.length;++i) {
        ch=s.charAt(i);
        if (ch=="/") t +="//"
        else if (ch==" ") t += "/b"
        else if (ch==",") t += "/."
        else if (ch==";") t += "/:"
        else if (ch=="\n") t += "/n"
        else if (ch=="\r") t += "/r"
        else if (ch=="\t") t += "/t"
        else if (ch=="\b") t += "/b"
        else t += ch;
    }
    return t;
 }

 function decode(s) {
    t="";
    if (s==null) return;
    for (var i=0;i<s.length;++i) {
        ch=s.charAt(i);
        if (ch=="/") {
           ++i;
           if (i<s.length) {
              ch=s.charAt(i);
              if (ch=="/") t += ch
              else if (ch==".") t += ","
              else if (ch==":") t += ";"
              else if (ch=="n") t += "\n"
              else if (ch=="r") t += "\r"
              else if (ch=="t") t += "\t"
              else if (ch=="b") t += " "
           }
        } else t += ch
    }
    return t;
 }

</script>

